﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using MVCForum.Mvc5.Identity.Models;
using mvcForum.DataProvider.EntityFramework;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using mvcForum.Core.Interfaces.DependencyManagement;
using mvcForum.Core.Interfaces.Data;

namespace MVCForum.Mvc5.Identity.MvcForumIdentity {

	/// <summary>
	/// Class that builds the dependencies needed for running Identity with MVC Forum
	/// </summary>
	public class IdentityBuilder : IDependencyBuilder {

		public virtual void Configure(IDependencyContainer container) {
			// Let's tell EF to create the database if it doesn't already exist
			Database.SetInitializer<IdentityDbContext>(new CreateDatabaseIfNotExists<IdentityDbContext>());

			// Add the IContext implementation for DI
			container.RegisterPerRequest<IContext, Context>();
			// Add the DbContext implementation for DI, adding the parameter for the constructor
			container.RegisterPerRequest<DbContext, IdentityDbContext>(new Dictionary<String, Object> { { "nameOrConnectionString", ConfigurationManager.ConnectionStrings["mvcForum.DataProvider.MainDB"].ConnectionString } });

			// Add the generic IRepository implementation for DI
			container.RegisterGeneric(typeof(IRepository<>), typeof(Repository<>));

			// Add the specific repositories
			new SpecificRepositoryConfiguration().Configure(container);
		}

		public void ValidateRequirements(IList<ApplicationRequirement> feedback) { }
	}
}